<?php
namespace app\usercp\controller;

use app\usercp\exception\UsercpException;
use think\Request;

class Banner extends Usercp
{
    public function initialize() {
        parent::initialize();
        $this->service = model('article/Banner', 'service');
    }

    /**
     * 导航栏列表
     */
    public function index(Request $request)
    {

        if ($request->isAjax()){
            $field = $this->request->param('field/s',true);
            $relations = array_filter($this->request->param('relations/a',[]));
            $attrs = array_filter($this->request->param('attrs/a',[]));
            $maps = [];
            $maps['current_id'] = $this->custom['current_id'];
            if ($request->has('site_id','param',true)){
                $maps['site_id'] = $request->param('site_id');
            }
            if ($request->has('keywords','param',true)){
                $maps['title'] = $request->param('keywords');
            }
            if ($request->has('status','param',true)){
                $maps['status'] = $request->param('status');
            }
            $list = $this->service->lists($maps,'update_time DESC',$this->page,$this->limit,$field,$relations,$attrs);
            if ($list->isEmpty()){
                return $this->result('',0,'暂无数据');
            }
            //注意：此处状态码一定和前端配置的状态码相同，否则数据会出问题(正常返回200，错误返回0)
            return $this->result($list,200);
        }
        return $this->fetch('index');
    }

    /**
     * 添加导航栏
     */
    public function add(Request $request)
    {

        if ($request->isAjax()){
            $validate = new \app\article\validate\Banner;
            if(!$validate->scene('create')->check($request->post())){
                return $this->result('',0,$validate->getError());
            }
            $data = [];
            $data['current_id'] = $this->custom['current_id'];
            $data['site_id'] = (string) $request->post('site_id');
            $data['title'] = (string) $request->post('title');
            $data['imgs'] =  $request->post('imgs');
            $data['content'] = $request->post('content');
            $data['sort'] = (int) $request->post('sort');
            $data['status'] = (int) $request->post('status');
            $result = $this->service->create($data);
            if ($result === false){
                return $this->result('',0,$this->service->getError());
            }
            return $this->result($result,200,'添加成功');
        }
        return $this->fetch('add');
    }

    /**
     * 编辑导航栏
     */
    public function edit(Request $request)
    {
        if ($request->isAjax()){
            $data = $request->post();
            $data['status'] = ($data['status'] == 'true') ? 1 : 0;
            $validate = new \app\article\validate\Banner;
            if(!$validate->scene('edit')->check($data)){
                return $this->result('',0,$validate->getError());
            }
            $result = $this->service->save($data,$data['id']);
            if ($result === false) {
                return $this->result('',0,$this->service->getError());
            }
            return $this->result($result,200,'修改成功');
        }
        return $this->fetch('edit');
    }

    /**
     * 删除导航栏
     */
    public function delete(Request $request)
    {
        if ($request->isAjax()) {
            $validate = new \app\article\validate\Banner;
            if(!$validate->scene('del')->check($request->post())){
                return $this->result('',0,$validate->getError());
            }
            $current_id = $this->custom['current_id'];
            $result = $this->service->destroy($request->post('id'), $current_id);
            if ($result === false) {
                return $this->result('',0,$this->service->getError());
            }
            return $this->result($result,200,'删除成功');
        }
        return $this->fetch('delete');
    }

}